define(function(require, exports, module) {
    'use strict';
    
    var rk = require('rk');
    

    $.widget('rk.taskCurtainCtrl', $.rk.oaCurtainCtrl, {
        _create: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            me._initTaskCurtain();
            elem.on('click', 'a[act="closeTaskCurtain"]', function () {
                me.closeMe();
            });
            elem.on('click', 'a[act=finishedGroupBy]', function () {
                var showType = $(this).attr('showType');
                me._loadDataForFinished(opt.viewType, showType);
                elem.find('a[act=finishedGroupBy]').removeClass('active');
                $(this).addClass('active');
            });
        },
        _init: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;
        },
        _initTaskCurtain: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            if (opt.aType != null) {
                me._loadDataForToDo(opt.aType);
            }
            if (opt.viewType != null) {
                me._loadDataForFinished(opt.viewType, 1);
            }
        },
        _loadDataForToDo: function (toDoType) {
            var me = this;
            var elem = me.element;
            rk.post('/json/oa_task/to-do-tasks.action', {
                toDoType: toDoType
            }).done(function (json) {
                if (json.status == 0) {
                    var listHtml = rk.templateText(require('oa/tmpl/task/tpl_task_curtain_to_do.tpl'), json.data);
                    elem.html(listHtml);
                    if (json.data.tasks.length < 1) {
                        $('span[act=taskTab].active').trigger('click');
                    }
                    elem.find('.oa-tab-body').beScroll();
                } else {
                    $.msg(rk.i18n('LOAD_ERROR'));
                }
            });
        },
        _loadDataForFinished: function (viewType, showType) {
            var me = this;
            var elem = me.element;
            if (viewType == 3 && elem.data('initAssignFinished')) {
                $('#myAssignedFinishedAccordionDiv').beLoading();
            }
            rk.post('/json/oa_task/finished-tasks.action', {
                viewType: viewType,
                showType: showType
            }).done(function (json) {
                if (json.status == 0) {
                    if (json.data.assignedUsers) {
                        $.each(json.data.assignedUsers, function (i, o) {
                            if (o.icon == '') {
                                o.icon = DEFAULTS.USER_DEFAULT_ICON;
                            }
                        });
                    }
                    me._renderFinishedCurtain(viewType, showType, json.data);
                } else {
                    $.msg(rk.i18n('LOAD_ERROR'));
                }
            });
        },
        _renderFinishedCurtain: function (viewType, showType, data) {
            var me = this;
            var elem = me.element;
            var listHtml = '';
            if (viewType == 1) {
                data.currentUserId = SESSION.user.id;
                listHtml = rk.templateText(require('oa/tmpl/task/tpl_task_curtain_my_finished.tpl'), data);
                elem.html(listHtml);
                if (data.allCount < 1) {
                    listHtml = rk.templateText(require('oa/tmpl/task/tpl_task_panel_default_list.tpl'), {});
                    elem.find('div.oa-curtain-body').html(listHtml);
                    return ;
                }
            } else if (viewType == 3) {
                if (!elem.data('initAssignFinished')) {
                    listHtml = rk.templateText(require('oa/tmpl/task/tpl_task_curtain_assign_finished.tpl'), data);
                    elem.html(listHtml);
                } else {
                    if(showType == 1){
                        listHtml = rk.templateText(require('oa/tmpl/task/tpl_task_panel_assign_by_user.tpl'), data);
                    }else if(showType == 2){
                        listHtml = rk.templateText(require('oa/tmpl/task/tpl_task_panel_assign_finished_by_time.tpl'), data);
                    }
                    $('#myAssignedFinishedAccordionDiv').html(listHtml);
                }
                elem.data('initAssignFinished', 1);
                if (data.allCount < 1) {
                    listHtml = rk.templateText(require('oa/tmpl/task/tpl_task_panel_default_list.tpl'), {});
                    elem.find('div.oa-curtain-body').html(listHtml);
                    return ;
                }
            }
            me._initAccordion();
            elem.find('.oa-task-content-list').beScroll();
        },
        _initAccordion: function () {
            var me = this;
            var elem = me.element;
            me.accordion = elem.find('div.approval-accordion:first');
            me.accordion.rkAccordion({
                heightStyle: "content"
            });
        }
    });

    return $;
});